using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace LxiDiscover
{
	public partial class FormMain : Form
	{
		ResExplorer m_ResExplorer = new ResExplorer();

		public FormMain()
		{
			InitializeComponent();
			m_ResExplorer.OnNewLxiDevice += new DiscoverHandle(ResExplorer_OnNewLxiDevice);
			m_ResExplorer.OnStart += new voidHandler(ResExplorer_OnStart);
			m_ResExplorer.OnStop += new voidHandler(ResExplorer_OnStop);

			Control.CheckForIllegalCrossThreadCalls = false;

			btnSearch_Click(null, null);
		}

		private void btnSearch_Click(object sender, EventArgs e)
		{
			btnSearch.Enabled = false;
			if(btnSearch.Text == "&Search")
			{
				listLxiDevice.Items.Clear();
				m_ResExplorer.Start();
			}
			else
			{
				m_ResExplorer.Stop();
			}
		}

		private void ResExplorer_OnNewLxiDevice(string strDescribe)
		{
			listLxiDevice.Items.Add(strDescribe);
		}

		void ResExplorer_OnStart()
		{
			btnSearch.Text = "S&top";
			btnSearch.Enabled = true;
		}

		void ResExplorer_OnStop()
		{
			btnSearch.Text = "&Search";
			btnSearch.Enabled = true;
		}

		private void btnExit_Click(object sender, EventArgs e)
		{
			Close();
		}

		private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
		{
			m_ResExplorer.Stop();
		}

		private void listLxiDevice_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			if(listLxiDevice.SelectedIndex >= 0)
			{
				Process.Start("IExplore.exe", "http://" + ResExplorer.GetIPstring(listLxiDevice.SelectedItem.ToString()));
			}
		}
	}
}